﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Configuration;
using System.Xml;
using Bingosoft.Common.Logging;

namespace HHMMS.FileServer
{
    /// <summary>
    /// 表示文件服务自定义配置字节。
    /// </summary>
	public class FileServerSection :  IConfigurationSectionHandler
	{
        #region Fields
		private static readonly ILog log = LogManager.GetCurrentClassLogger();
        private static FileServerSection _config;
        #endregion

        #region Methods
        public object Create(object parent, object configContext, XmlNode section)
		{
            //获取文件服务基本信息
            this.DefaultProviderName = XmlNodeHelper.GetStringValue(section, "@defaultProvider", string.Empty);
            this.Url = XmlNodeHelper.GetStringValue(section, "@url", string.Empty);
            this.AcceptFileTypes = XmlNodeHelper.GetStringValue(section, "@acceptFileTypes", ".*");
            this.BlockSize = XmlNodeHelper.GetIntValue(section, "@blockSize", 65536);
            this.MaxFileSize = XmlNodeHelper.GetIntValue(section, "@maxFileSize", 102400);
            this.ConfigSection = section;

            //从配置节中初始化文件服务器验证信息
            XmlNode providerNode = null;
            if (string.IsNullOrEmpty(this.DefaultProviderName))
            {
                providerNode = section.SelectSingleNode("providers/add[1]");
            }
            else
            {
                providerNode = section.SelectSingleNode("providers/add[@name='" + this.DefaultProviderName + "']");
            }

            if (providerNode != null)
            {
                this.DefaultProviderTypeName = XmlNodeHelper.GetStringValue(providerNode, "@type", string.Empty);
                this.UserName = XmlNodeHelper.GetStringValue(providerNode, "@userName", string.Empty);
                this.Password = XmlNodeHelper.GetStringValue(providerNode, "@password", string.Empty);
                this.UseDefaultCredential = XmlNodeHelper.GetBooleanValue(providerNode, "@useDefaultCredential", true);
            }
            else
            {
                this.DefaultProviderTypeName = "HHMMS.FileServer.FileServerProvider,HHMMS.FileServer";
            }
            return this;
		}
        #endregion

        #region Properties
        /// <summary>
        /// 配置节点。
        /// </summary>
        public XmlNode ConfigSection
        {
            get;
            set;
        }

        /// <summary>
        /// 默认提供查询名称。
        /// </summary>
        public string DefaultProviderName
        {
            get;
            set;
        }

        /// <summary>
        /// 文件服务器url
        /// </summary>
        public string Url
        {
            get;
            set;
        }

        /// <summary>
        /// 文件读取或者写入时缓冲的字节数，默认为65536Byte(64KB)。
        /// </summary>
        public int BlockSize
        {
            get;
            set;
        }

        /// <summary>
        /// 最大允许的文件大小，单位为KB，默认为102400KB(100MB)。
        /// </summary>
        public int MaxFileSize
        {
            get;
            set;
        }

        /// <summary>
        /// 允许文件服务器接收的文件类型。
        /// </summary>
        public string AcceptFileTypes
        {
            get;
            set;
        }

        /// <summary>
        /// 文件服务默认提供程序。
        /// </summary>
        public string DefaultProviderTypeName
        {
            get;
            set;
        }

        /// <summary>
        /// 文件服务器用户名。
        /// </summary>
        public string UserName
        {
            get;
            set;
        }

        /// <summary>
        /// 文件服务器密码。
        /// </summary>
        public string Password
        {
            get;
            set;
        }

        public bool UseDefaultCredential
        {
            get;
            set;
        }

        /// <summary>
        /// 获取默认配置。
        /// </summary>
        public static FileServerSection Current
		{
			get
			{
				if (_config == null)
				{
                    _config = ConfigurationManager.GetSection("fileServer") as FileServerSection;
				}
				if (_config == null)
				{
                    throw new Exception("找不到文件服务自定义配置字节：fileServer");
				}
				return _config;
			}
        }
        #endregion
    }
}
